package com.sso.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sso.entity.Maps;
import com.sso.entity.Report;
import com.sso.entity.Sample;
import com.sso.entity.Task;
import com.sso.feign.UserFeignService;
import com.sso.filter.R;
import com.sso.form.AddSampleForm;
import com.sso.service.MapsService;
import com.sso.service.ReportService;
import com.sso.service.SampleService;
import com.sso.service.TaskService;
import com.sso.service.impl.UserImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xia
 * @since 2022-07-23
 */
@RestController
@CrossOrigin
@Api(tags = "样品接口")
@RequestMapping("/shale_porosity/sample")
public class SampleController {

    @Autowired
    private SampleService sampleService;
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private UserImpl userImpl;

    @PostMapping("add")
    @ApiOperation("上传样品数据")
    public R addUser(@Valid AddSampleForm addSampleForm) {
        Sample sample = new Sample();
        BeanUtils.copyProperties(addSampleForm, sample);
        sample.setUserId(userImpl.get().getId());
        sampleService.save(sample);
        return R.ok();
    }

    @PutMapping("update")
    @ApiOperation("修改样品数据")
    public R updateUser(@Valid AddSampleForm updateSampleForm,Integer id) {
        Sample sample = new Sample();
        BeanUtils.copyProperties(updateSampleForm, sample);
        sample.setUserId(userImpl.get().getId());
        sample.setId(id);
        sampleService.updateById(sample);
        return R.ok();
    }
    @Autowired
    private MapsController mapsController;
    @DeleteMapping("/{id}")
    @ApiOperation("删除样品,同时会删除所有和此样品相关的所有图像信息")
    public R delUser(@PathVariable Long id) {
        if(reportService.getOne(new LambdaQueryWrapper<Report>().eq(Report::getPictureId,id))!=null) {
            reportService.remove(new LambdaQueryWrapper<Report>().eq(Report::getPictureId, id));
        }
        if(taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getPictureId,id))!=null) {
            taskService.remove(new LambdaQueryWrapper<Task>().eq(Task::getPictureId, id));
        }
        mapsController.delUser(id);
        boolean b = sampleService.removeById(id);
        if (!b) {
            return R.error("删除样品失败，请重试");
        }
        return R.ok();
    }

    @GetMapping("/text")
    @ApiOperation("条件查询")
    public R selectByAll(@Valid AddSampleForm querySampleForm){
        QueryWrapper<Sample> queryWrapper = new QueryWrapper<Sample>();
        if(querySampleForm.getRegion()!=null){
            queryWrapper.like("region",querySampleForm.getRegion());
        }
        if(querySampleForm.getName()!=null){
            queryWrapper.like("name",querySampleForm.getName());
        }
        if(querySampleForm.getWellBlock()!=null){
            queryWrapper.like("well_block",querySampleForm.getWellBlock());
        }
        if(querySampleForm.getWellDeep()!=null){
            queryWrapper.like("well_deep",querySampleForm.getWellDeep());
        }
        if(querySampleForm.getWellLayer()!=null){
            queryWrapper.like("well_layer",querySampleForm.getWellLayer());
        }
        if(querySampleForm.getInfo()!=null){
            queryWrapper.like("info",querySampleForm.getInfo());
        }
        if(querySampleForm.getWellNum()!=null){
            queryWrapper.like("well_num",querySampleForm.getWellNum());
        }
//        queryWrapper.lambda()
//                .like(Sample::getRegion, querySampleForm.getRegion())
//                .like(Sample::getName, querySampleForm.getName())
//                .like(Sample::getWellBlock, querySampleForm.getWellBlock())
//                .like(Sample::getWellDeep, querySampleForm.getWellDeep())
//                .like(Sample::getWellLayer, querySampleForm.getWellLayer())
//                .like(Sample::getWellNum, querySampleForm.getWellNum());
        List<Sample> list = sampleService.list(queryWrapper);
        return R.ok().put("raw",list);
    }

    @GetMapping("/{current}/{limit}")
    @ApiOperation("分页获取所有样品信息")
    public R listUsers(@PathVariable Long current, @PathVariable Long limit) {
        Page<Sample> page = new Page<>(current, limit);
        QueryWrapper<Sample> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        sampleService.page(page,queryWrapper);
        long total=page.getTotal();
        return R.ok().put("total",total).put("raw",page.getRecords());
    }
}
